<template>
  <div class="course-list">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span>数据筛选</span>
        <div class="search">
          <div>
            <span>课程名称：&emsp;</span>
            <el-input
              placeholder="课程名称"
              v-model="conditionObj.courseName"
              clearable
            >
            </el-input>
          </div>
          <div>
            <span>课程状态：&emsp;</span>
            <el-select placeholder="课程状态" v-model="conditionObj.status">
              <el-option label="请选择" value=""> </el-option>
              <el-option label="上架" :value="1"> </el-option>
              <el-option label="下架" :value="0"> </el-option>
            </el-select>
          </div>
          <div class="action">
            <el-button @click="resetCondition">重置</el-button>
            <el-button :loading="isLoading" @click="searchCourse" type="primary"
              >查询搜索</el-button
            >
          </div>
        </div>
        <el-button class="mt30" @click="$router.push({ name: 'update-course' })"
          >添加课程</el-button
        >
      </div>
      <el-table :data="courses" style="width: 100%">
        <el-table-column prop="id" label="编号" width="120"> </el-table-column>
        <el-table-column prop="courseName" label="课程名称" width="240">
        </el-table-column>
        <el-table-column prop="price" label="课程价格" width="180">
        </el-table-column>
        <el-table-column prop="sortNum" label="排序" width="180">
        </el-table-column>
        <el-table-column prop="status" label="状态" width="180">
          <template slot-scope="scope">
            <span v-if="scope.row.status" class="course-dot success"></span>
            <span v-else class="course-dot danger"></span>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              @click="manageCourse(scope.row)"
              :type="scope.row.status ? 'danger' : 'success'"
              >{{ scope.row.status ? '下架' : '上架' }}</el-button
            >
            <el-button
              @click="
                $router.push({
                  name: 'edit-course',
                  params: { courseId: scope.row.id }
                })
              "
              >编辑</el-button
            >
            <el-button
              @click="
                $router.push({ name: 'section', params: { id: scope.row.id } })
              "
              >内容管理</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        background
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-size="10"
        layout="prev, pager, next"
        :total="total"
      >
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
import { getCourseList, changeCourseState } from '@/service/course'
export default {
  name: 'CourseList',
  data () {
    return {
      isLoading: false,
      courseName: '',
      currentPage: 1,
      total: 0,
      conditionObj: {
        currentPage: 1,
        pageSize: 10,
        courseName: '',
        status: ''
      },
      courses: [],
      courseId: ''
    }
  },
  created () {
    this.loadCourse()
  },
  methods: {
    // 新增或编辑课程操作成功
    onSuccess () {
      this.dialogFormVisible = false
      this.loadCourse()
      // 操作完成后课程id初始化
      this.courseId = ''
    },
    // 新增或编辑课程取消操作
    onCancel () {
      this.dialogFormVisible = false
      // 操作完成后课程id初始化
      this.courseId = ''
      // this.loadAllcourse()
    },
    // 打开课程新增或编辑对话框
    openDialog () {
      this.isEdit = false
      this.dialogFormVisible = true
    },
    // 改变课程上下架状态
    async manageCourse (row) {
      const { data } = await changeCourseState({
        courseId: row.id,
        status: row.status ^ 1
      })
      if (data.code === '000000') {
        this.$message.success('操作成功')
        this.loadCourse()
      }
    },
    // 编辑
    editCourse (row) {
      this.dialogFormVisible = true
      this.isEdit = true
      this.courseId = row.id
      //   console.log(row)
    },
    // 重置搜索条件
    resetCondition () {
      // 搜索条件初始化
      this.conditionObj.courseName = ''
      this.conditionObj.status = ''
      this.conditionObj.current = 1
      this.loadCourse()
    },
    // 加载课程列表
    async loadCourse () {
      const { data } = await getCourseList(this.conditionObj)
      if (data.code === '000000') {
        // this.$message.success('查询成功')
        // console.log(data.data)
        this.courses = data.data.records ?? []
        this.currentPage = data.data.current
        this.total = data.data.total
      }
      this.isLoading = false
    },
    // 查询课程
    async searchCourse () {
      this.isLoading = true
      // 页码重置
      this.conditionObj.current = 1
      // this.conditionObj.size = 10
      this.loadCourse()
    },
    handleCurrentChange (val) {
      console.log(`当前页: ${val}`)
      this.conditionObj.currentPage = val
      this.loadCourse()
    }
  }
}
</script>

<style lang="scss" scoped>
.mt30 {
  margin-top: 30px;
}
.clearfix:after {
  clear: both;
}

.box-card {
  text-align: left;
  .action {
    float: right;
    .el-button {
      margin-left: 16px;
    }
  }
  .search {
    display: flex;
    align-items: center;
    margin-top: 30px;
    > div {
      // display: flex;
      margin-left: 20px;
      > span {
        width: 100px;
      }
      .el-input {
        width: 200px;
      }
    }
  }
  .el-table {
    .course-dot {
      display: inline-block;
      min-width: 15px;
      width: 15px;
      height: 15px;
      border-radius: 50%;
      &.danger {
        background-color: $danger-color;
      }
      &.success {
        background-color: $success-color;
      }
    }
  }
  .el-pagination {
    margin-top: 20px;
  }
}
</style>
